package com.qf.java2201.listener;

import com.qf.java2201.dao.TypeDao;
import com.qf.java2201.dao.impl.TypeDaoImpl;
import com.qf.java2201.pojo.HNews;
import com.qf.java2201.pojo.HType;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.List;

@WebListener
public class AppListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sec) {
        System.out.println("应用初始化");

        //准备数据字典的数据
        TypeDao typeDao = new TypeDaoImpl();

       List<HType> htypelist=  typeDao.findAllTypes();
       List<HNews> newslist=  typeDao.findAllNews();


       //把数据字典的数据放在servletContrxt 中
        ServletContext servletContext = sec.getServletContext();

        //讲数据字典集合放入域对象
        servletContext.setAttribute("ts", htypelist);

        //准备新闻列表数据 也放在域对象
        servletContext.setAttribute("hs", newslist);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
